package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件（上传）存储至本地服务器
 */
//@Slf4j
//@RestController
//public class UploadController {
//    @PostMapping("/upload")
//    public Result uploadFile(String username, Integer age,
//                             MultipartFile image){//image是要进行上传的文件，得用这个类
//        log.info("username:{},age:{},上传文件是：{}",username,age,image);
//        return Result.success();
//    }
//}

/**
 * 借助阿里云OOS服务上传文件(需要创建阿里云账户，我就不创建测试了，先写代码）
 */
@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils0;

    @PostMapping("/upload")
    public Result uploadFile(MultipartFile image) throws IOException {
        log.info("上传文件{}",image);
        String imgUrl = aliOSSUtils0.upload(image);//返回的是一个url
        log.info("上传文件返回的url：{}",imgUrl);
        return Result.success(imgUrl);//因为响应里面data是url
    }

}
